package ua.kharkiv.karimov.knure.SummaryTask1.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.InetAddress;
import java.net.Socket;

/**
 * This class must be running on client side
 * Connect to localhost. Every 10 second send test messages
 * @author Karimov
 *
 */
public class Client {
	public static int updateTime = 10 * 1000; // every 10 seconds
	public static boolean state = true;
	public String myName = "controller";

	private static class ClientThreadRead extends Thread {
		public static Socket socket;
		public static BufferedReader in;
		public static PrintWriter out;
		public String myName;

		public ClientThreadRead(String name) throws IOException {
			super();
			myName = name;
			InetAddress addr = InetAddress.getByName(null);
			socket = new Socket(addr,
					ua.kharkiv.karimov.knure.SummaryTask1.server.Server.PORT);
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
					socket.getOutputStream())), true);
			start();
		}

		public void run() {
			try {
				out.println(myName);
				out.println(myName);
				while (state) {
					if (socket.isClosed())
						break;
					String str = in.readLine();
					if (str == null)
						continue;
					if (str.indexOf("update") > -1)
						continue;
					String[] strings = str.split("\t");
					for (String s : strings)
						System.out.println(s);
					if (str != null && str.equals("exit"))
						if (ClientThreadRead.out != null) {
							try {
								state = false;
								ClientThreadRead.socket.close();
								break;
							} catch (IOException e1) {
								e1.printStackTrace();
							}
						}

				}
				System.out.println("closing...");
			} catch (IOException e) {
				try {
					state = false;
					socket.close();
				} catch (IOException ee) {
					System.err.println("Socket not closed");
				}
			} finally {
				try {
					state = false;
					socket.close();
				} catch (IOException e) {
					System.err.println("Socket not closed");
				}
			}
		}
	}

	private class ClientThreadWrite extends Thread {
		public void run() {
			String str;
			Reader consoleReader = new InputStreamReader(System.in);
			BufferedReader br = new BufferedReader(consoleReader);
			while (state) {
				try {
					sleep(2000);
					str = br.readLine();
					if (str == null)
						continue;
					ClientThreadRead.out.println(str);
					if (str.toLowerCase().equals("exit"))
						state = false;
				} catch (InterruptedException | IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private class ClientThreadUpdate extends Thread {
		public void run() {
			while (state) {
				try {
					sleep(updateTime);
					ClientThreadRead.out.println("update");
					ClientThreadRead.out.println("weight_capacity");
					ClientThreadRead.out.println("get fuelkm 15 19");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public Client() {
		super();
		try {
			new ClientThreadRead(myName);
			new ClientThreadWrite().start();
			new ClientThreadUpdate().start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws IOException {
		new Client();
	}
}
